package com.udp.nml;

import java.net.InetSocketAddress;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Example UDP Client implementing Apache Mina IoHandler
 * 
 * @author Niall McLoughlin
 */
public class UDPClient implements IoHandler
{
	static Logger logger = LoggerFactory.getLogger(UDPClient.class);
	
	NioDatagramConnector connector;
	
	IoSession session;

	public static void main(String[] args)
	{
		logger.info(">>> main");
		
		UDPClient udp = new UDPClient();
		udp.connect();
		
		logger.info("<<< connect");
	}
	
	public void connect()
	{
		logger.info(">>> connect");
		
		connector = new NioDatagramConnector();
		connector.setHandler( this );
		ConnectFuture connFuture = connector.connect( new InetSocketAddress("localhost", UDPServer.PORT ));
		
		connFuture.addListener( new IoFutureListener(){
            public void operationComplete(IoFuture future) 
            {
                ConnectFuture connFuture = (ConnectFuture)future;
                
                if(connFuture.isConnected())
                {
                    session = future.getSession();
                    try 
                    {
                        sendData();
                    } 
                    catch (InterruptedException e) 
                    {
                        e.printStackTrace();
                    }
                } 
                else 
                {
                	logger.info("not connected");
                }
            }
        });
		
		logger.info("<<< connect");
	}
	
	/**
	 * Send some data.
	 */
	private void sendData() throws InterruptedException 
	{
		logger.info(">>> sendData");
		
        long free = Runtime.getRuntime().freeMemory();
        
        IoBuffer buffer = IoBuffer.allocate(8);
        buffer.putLong(free);
        buffer.flip();
        session.write(buffer);
		
		logger.info("<<< sendData");
	}
	
	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception 
	{
		logger.info(">>> exceptionCaught");
		
		logger.info("<<< exceptionCaught");
	}
	@Override
	public void messageReceived(IoSession arg0, Object arg1) throws Exception
	{
		logger.info(">>> messageReceived");
		
		logger.info("<<< messageReceived");
	}
	@Override
	public void messageSent(IoSession arg0, Object arg1) throws Exception 
	{
		logger.info(">>> messageSent");
		
		logger.info("<<< messageSent");
	}
	
	@Override
	public void sessionClosed(IoSession arg0) throws Exception
	{
		logger.info(">>> sessionClosed");
		
		logger.info("<<< sessionClosed");
	}
	
	@Override
	public void sessionCreated(IoSession arg0) throws Exception 
	{
		logger.info(">>> sessionCreated");
		
		logger.info("<<< sessionCreated");		
	}
	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception 
	{
		logger.info(">>> sessionIdle");
		
		logger.info("<<< sessionIdle");
	}
	
	@Override
	public void sessionOpened(IoSession arg0) throws Exception 
	{
		logger.info(">>> sessionOpened");
		
		logger.info("<<< sessionOpened");		
	}
}
